package com.njupt.sellsec.util;


import org.apache.commons.codec.digest.DigestUtils;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 10:00 2018/7/28
 * @Modified By:
 **/
public class MD5Util {
    public static String md5(String src){
        return DigestUtils.md5Hex(src);
    }

    public static final String salt = "qwertyuiop";

    /**
     * 表单输入的密码先进行一次固定salt的MD5加密
     * @param inputPass
     * @return
     */
    public static String inputPassToFormPass(String inputPass){
        String src  = ""+salt.charAt(0)+salt.charAt(3)+salt.charAt(5)+inputPass+salt.charAt(6)+salt.charAt(7);
        System.out.println(src);
        return md5(src);
    }

    /**
     * 表单提交给后台的密码再次进行一个随机salt的MD5加密，然后加入DB
     * @param formPass
     * @param salt
     * @return
     */
    public static String formPassToDBPass(String formPass,String salt){
        String src  = ""+salt.charAt(0)+salt.charAt(3)+salt.charAt(5)+formPass+salt.charAt(6)+salt.charAt(7);
        return md5(src);
    }

    /**
     * 表单输入数据到DB数据的两次加密
     * @param inputPass
     * @param salt
     * @return
     */
    public static String inputPassToDBPass(String inputPass,String salt){
        String formPass = inputPassToFormPass(inputPass);
        String dbPass = formPassToDBPass(formPass,salt);
        return dbPass;
    }


    public static void main(String[] args) {
        System.out.println(inputPassToDBPass("123456","qwertyuiop"));
    }
}
